package org.lsb.app_back.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Map;

@Component
public class JwtUtil {
    private static String singningKey = "TestPeo";
    private static long expiration = 43200000L;


    /**
     * 生成Jwt令牌
     * @param claims
     * @return
     */
    public static String generateJwt(Map<String,Object> claims){

        String jwt = Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256, singningKey)
                .setExpiration(new Date(System.currentTimeMillis() + expiration))
                .compact();
        return jwt;

    }

    /**\
     * 解析Jwt令牌
     * @param jwt
     * @return
     */
    public static Claims parseJwt(String jwt){
        Claims claims = Jwts.parser()
                .setSigningKey(singningKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }


}
